package com.jaya.budan.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.google.gson.Gson;
import com.jaya.budan.R;
import com.jaya.budan.base.BaseActivity;
import com.jaya.budan.connect.RequestConnect;
import com.jaya.budan.utils.ActivityManger;
import com.jaya.budan.utils.CacheUtil;
import com.jaya.budan.utils.ToolUtils;
import com.jaya.budan.utils.Utils;
import com.jaya.budan.utils.VolleyUtil;
import com.tencent.imsdk.TIMCallBack;
import com.tencent.imsdk.TIMManager;
import com.tencent.imsdk.v2.V2TIMManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

public class Set_Activity extends BaseActivity implements View.OnClickListener {

    private RelativeLayout back, rl_set_zhanghao, rl_set_out_login, rl_set_message_tx, rl_set_about_budan, rl_set_hide, rl_set_clear_hc;
    private String access_token, username;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        Intent intent = getIntent();
        access_token = intent.getStringExtra("access_token");
        username = intent.getStringExtra("username");
        back = findViewById(R.id.back);
        rl_set_zhanghao = findViewById(R.id.rl_set_zhanghao);
        rl_set_out_login = findViewById(R.id.rl_set_out_login);
        rl_set_message_tx = findViewById(R.id.rl_set_message_tx);
        rl_set_about_budan = findViewById(R.id.rl_set_about_budan);
        rl_set_hide = findViewById(R.id.rl_set_hide);
        rl_set_clear_hc = findViewById(R.id.rl_set_clear_hc);
        back.setOnClickListener(this);
        rl_set_zhanghao.setOnClickListener(this);
        rl_set_out_login.setOnClickListener(this);
        rl_set_message_tx.setOnClickListener(this);
        rl_set_about_budan.setOnClickListener(this);
        rl_set_hide.setOnClickListener(this);
        rl_set_clear_hc.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
            case R.id.rl_set_zhanghao:
                Intent intent1 = new Intent(this, Zhanghao_Safe_Activity.class);
                intent1.putExtra("username", username);
                startActivity(intent1);
                break;
            case R.id.rl_set_out_login:
                postData();
                break;
            case R.id.rl_set_message_tx:
                Intent intent2 = new Intent(this, Message_TX_Activity.class);
                intent2.putExtra("access_token", access_token);
                startActivity(intent2);
                break;
            case R.id.rl_set_about_budan:
                Intent intent3 = new Intent(this, About_US_Activity.class);
                intent3.putExtra("access_token", access_token);
                startActivity(intent3);
                break;
            case R.id.rl_set_hide:
                Intent intent4 = new Intent(this, Set_Hide_Activity.class);
                intent4.putExtra("access_token", access_token);
                startActivity(intent4);
                break;
            case R.id.rl_set_clear_hc:
                try {
                    String cache = CacheUtil.getTotalCacheSize(this);
                    ToolUtils.showToast(this, cache);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                CacheUtil.clearAllCache(this);
                break;
            default:
                break;
        }
    }

    /**
     * 网络请求
     */
    private void postData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, RequestConnect.out_login_Url + access_token, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Log.d("register", jsonObject.toString());
                try {
                    int code = jsonObject.optInt("code");
                    int status = jsonObject.optInt("status");
                    String message = jsonObject.getString("message");
                    if (code == 0) {
                        Utils.logOut(Set_Activity.this);
//                        System.exit(0);
                        Intent intent = new Intent(Set_Activity.this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                    } else {
                        ToolUtils.showToast(Set_Activity.this, message);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                ToolUtils.showToast(Set_Activity.this, "网络请求失败，请稍后重试~");
            }
        });
        request.setTag(this);
        VolleyUtil.getQueue(this).add(request);
    }
}
